home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / util / netstat / netstat
Text File  |  1994-04-22  |  12KB  |  294 lines

  1. /* sys:rexxc/rx
  2.  * Hey Emacs! This file is -*- rexx -*-
  3.  * $Id: netstat,v 3.2 1994/04/22 15:11:55 jraja Exp $
  4.  * $VER: netstat 3.1 (22.4.94)
  5.  * Copyright © 1993, 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  6.  *                  Helsinki University of Technology, Finland.
  7.  *                  All rights reserved.
  8.  */
  9.  
  10. ARG opt args
  11. if left(opt,1)~='-' then do
  12.   args=opt
  13.   opt=''
  14. end
  15. args=strip(args)
  16. numeric=0
  17. if pos('N',opt)~=0 then numeric=1
  18.  
  19. resolved. = ''
  20. ALL = 0
  21. tab="    "
  22.  
  23. if args = "ALL" then ALL=1 /* All information wanted */
  24.  
  25. options results /* Results from external command wanted */
  26. address AMITCP /* default command host is AMITCP */
  27.  
  28. RESULT = "" /* Set so that it don't get value RESULT */
  29.  
  30. if args = "" | args = "ALL" then do
  31.   /* States of TCP finite state machine */
  32.   state.0 = "CLOSED"
  33.   state.1 = "LISTEN"
  34.   state.2 = "SYN_SENT"
  35.   state.3 = "SYN_RECV"
  36.   state.4 = "ESTABLISHED"
  37.   state.5 = "CLOSE_FIN"
  38.   state.6 = "CLOSED_FIN"
  39.   state.7 = "FINACK_WAIT"
  40.   state.8 = "CLOSED_FINACK"
  41.   state.9 = "CLOSED"
  42.   state.A = "TIME_WAIT"
  43.  
  44.   'QUERY' 'CONNECTIONS' /* Ask information */
  45.  
  46.   /* Print header */
  47.   say "Proto Recv-Q Send-Q  Local Address           Foreign Address        (status)"
  48.  
  49.   rest=result
  50.   do while rest ~= ""
  51.   parse upper value rest with pr rq sq la lp fa fp st rest
  52.   /* Parse result string */
  53.   if pr=t then do
  54.     pro = "tcp" /* Protocol */
  55.     sts = state.st /* Name of state */
  56.   end
  57.   else do
  58.     pro = "udp"
  59.     sts = "" /* udp is stateless */
  60.   end
  61.   rqd = x2d(rq) /* Queue lengths */
  62.   sqd = x2d(sq)
  63.   /* Addresses to decimal dot notation */
  64.   lad = x2d(substr(la,1,2)) || "." ||x2d(substr(la,3,2)),
  65.     || "." ||x2d(substr(la,5,2)) || "." ||x2d(substr(la,7,2))
  66.   fad = x2d(substr(fa,1,2)) || "." ||x2d(substr(fa,3,2)),
  67.     || "." ||x2d(substr(fa,5,2)) || "." ||x2d(substr(fa,7,2))
  68.   lpd = x2d(lp) /* Port numbers to decimal */
  69.   fpd = x2d(fp)
  70.   /* Print only if connection is non-listening or all connections wanted */
  71.   if all = 1 | (st ~= 0 & st ~= 1) then do
  72.     sockst = left(pro, 5) right(rqd, 6) right(sqd, 6),
  73.       left("  " || resolve(lad) || "." || lpd, 23),
  74.       left("  " || resolve(fad) || "." || fpd, 23) "" sts
  75.     say sockst
  76.   end
  77. end /* while */
  78. end /* connections */
  79.  
  80. /* If status wanted */
  81. if left(args,6) = "STATUS" then do
  82.  
  83.   /* Ask ICMP information */
  84.   'Q' ICMP CHksum ICMP COde ICMP Error ICMP Icmpold ICMP Length ICMP Tooshort ICMP Responses ICMP Shortold
  85.   /* Parse ICMP information */
  86.   parse value result with icps_checksum icps_badcode icps_error icps_oldicmp icps_badlen icps_reflect icps_oldshort icps_tooshort .
  87.  
  88.   /* Ask and parse IP information */
  89.   'Q' IP CH IP D IP FC IP FD IP FE IP FO IP FS IP FT IP FW IP H IP LE IP LO IP N IP OD IP OF IP REA IP RED IP TOOSH  IP TOOSM IP TOTAL
  90.   parse value result with ips_badsum ips_delivered ips_cantfragment ips_fragdropped ips_fragmented ips_forward ips_fragments ips_fragtimeout ips_cantforward ips_badhlen ips_badlen ips_localout ips_noproto ips_odropped ips_ofragments ips_reassembed ips_redirectsent ips_tooshort ips_toosmall ips_total .
  91.  
  92.   /* Ask and parse TCP information */
  93.   'Q' TCP Accepts TCP CAttem TCP CDrops TCP CLosed TCP COnnect TCP DElack TCP DRops TCP KADrops TCP KAProbe  TCP KATimeo TCP Persist TCP RACKByte TCP RACKPack TCP RACKToom TCP RAFter TCP RBLate TCP RBYte TCP RChksum TCP RDUPAck TCP RDUPByte TCP RDUPPack TCP ROFfset TCP ROOByte TCP ROOPack TCP RPAck TCP RPDUPByte TCP RPDUPData TCP RPLate TCP RPShort TCP RTOtal TCP RTTupdate TCP RWProbe TCP RWUpdate TCP SAcks TCP SByte TCP SCtrl TCP SEgstimed TCP SPack TCP SREByte TCP SREPack TCP STotal TCP SUrgent TCP SWProbe TCP SWUpdate TCP Timeodrop TCP RExmtt
  94.   parse value result with tcps_accepts tcps_connattempt tcps_conndrops tcps_closed tcps_connects tcps_delack tcps_drops tcps_keepdrops tcps_keepprobe tcps_keeptimeo tcps_persisttimeo tcps_rcvackbyte tcps_rcvackpack tcps_rcvacktoomuch tcps_rcvafterclose tcps_rcvbyteafterwin tcps_rcvbyte tcps_rcvbadsum tcps_rcvdupack tcps_rcvdupbyte tcps_rcvduppack tcps_rcvbadoff tcps_rcvoobyte tcps_rcvoopack tcps_rcvpack tcps_rcvpartdupbyte tcps_rcvpartduppack tcps_rcvpackafterwin tcps_rcvshort tcps_rcvtotal tcps_rttupdated tcps_rcvwinprobe tcps_rcvwinupd tcps_sndacks tcps_sndbyte tcps_sndctrl tcps_segstimed tcps_sndpack tcps_sndrexmitbyte tcps_sndrexmitpack tcps_sndtotal tcps_sndurg tcps_sndprobe tcps_sndwinup tcps_timeoutdrop tcps_rexmttimeo .
  95.  
  96.   /* Ask and parse UDP information */
  97.   'Q' UDP Bcnoport UDP Chksum UDP Fullsoc UDP Headshort UDP Itotal UDP Length UDP Mispcb UDP Noport UDP Ototal
  98.   parse value result with udps_noportbcast udps_badsum udps_fullsock udps_hdrops udps_ipackets udps_badlen udps_pcbcachemiss udps_noport udps_opackets .
  99.  
  100.   /* Ask ICMP history */
  101.   'Q' ICMPHIST
  102.   icmphist = result
  103.  
  104.   say "tcp:"
  105.   say tab || tcps_sndtotal "packets sent"
  106.   say tab || tab || tcps_sndpack "data packets ("||tcps_sndbyte "bytes)"
  107.   say tab || tab || tcps_sndrexmitpack "data packets ("||tcps_sndrexmitbyte "bytes) retransmitted"
  108.   say tab || tab || tcps_sndacks "ack-only packets ("|| tcps_delack "delayed)"
  109.   say tab || tab || tcps_sndurg "URG only packets"
  110.   say tab || tab || tcps_sndprobe "window probe packets"
  111.   say tab || tab || tcps_sndwinup "window update packets"
  112.   say tab || tab || tcps_sndctrl "control packets"
  113.   say tab || tcps_rcvtotal "packets received"
  114.   say tab || tab || tcps_rcvackpack "acks (for" tcps_rcvackbyte "bytes)"
  115.   say tab || tab || tcps_rcvdupack  "duplicate acks"
  116.   say tab || tab || tcps_rcvacktoomuch "acks for unsent data"
  117.   say tab || tab || tcps_rcvpack "packets ("|| tcps_rcvbyte "bytes) received in-sequence"
  118.   say tab || tab || tcps_rcvduppack "completely duplicate packets ("|| tcps_rcvdupbyte "bytes)"
  119.   say tab || tab || tcps_rcvpartduppack "packets with some dup. data ("|| tcps_rcvpartdupbyte "bytes duped)"
  120.   say tab || tab || tcps_rcvoopack "out-of-order packets ("||tcps_rcvoobyte "bytes)"
  121.   say tab || tab || tcps_rcvpackafterwin "packets ("||tcps_rcvbyteafterwin "bytes) of data after window"
  122.   say tab || tab || tcps_rcvwinprobe "window probes"
  123.   say tab || tab || tcps_rcvwinupd "window update packets"
  124.   say tab || tab || tcps_rcvafterclose "packets received after close"
  125.   say tab || tab || tcps_rcvbadsum "discarded for bad checksum"
  126.   say tab || tab || tcps_rcvbadoff "discarded for bad header offset fields"
  127.   say tab || tab || tcps_rcvshort "discarded because packet too short"
  128.   say tab || tcps_connattempt "connection requests"
  129.   say tab || tcps_accepts "connection accepts"
  130.   say tab || tcps_connects "connections established (including accepts)"
  131.   say tab || tcps_closed "connections closed (including" tcps_drops "drops)"
  132.   say tab || tcps_conndrops "embryonic connections dropped"
  133.   say tab || tcps_rttupdated "segments updated rtt (of" tcps_segstimed "attempts)"
  134.   say tab || tcps_rexmttimeo "retransmit timeouts"
  135.   say tab || tab || tcps_timeoutdrop "connections dropped by rexmit timeout"
  136.   say tab || tcps_persisttimeo "persist timeouts"
  137.   say tab || tcps_keeptimeo "keepalive timeouts"
  138.   say tab || tab || tcps_keepprobe "keepalive probes sent"
  139.   say tab || tab || tcps_keepdrops "connections dropped by keepalive"
  140.  
  141.   say "udp:"
  142.   say tab || udps_hdrops "incomplete headers"
  143.   say tab || udps_badlen "bad data length fields"
  144.   say tab || udps_badsum "bad checksums"
  145.  
  146.   say "ip:"
  147.   say tab || ips_total "total packets received"
  148.   say tab || ips_badsum "bad header checksums"
  149.   say tab || ips_tooshort "with size smaller than minimum"
  150.   say tab || ips_toosmall "with data size < data length"
  151.   say tab || ips_badhlen "with header length < data size"
  152.   say tab || ips_badlen "with data length < header length"
  153.   say tab || ips_fragments "fragments received"
  154.   say tab || ips_fragdropped "fragments dropped (dup or out of space)"
  155.   say tab || ips_fragtimeout "fragments dropped after timeout"
  156.   say tab || ips_forward "packets forwarded"
  157.   say tab || ips_cantforward "packets not forwardable"
  158.   say tab || ips_redirectsent "redirects sent"
  159.  
  160.   /* ICMP codes */
  161.   icmph. = "UNKNOWN" /* default */
  162.   icmph.0 = "Echo reply"
  163.   icmph.3 = "Destination unreachable"
  164.   icmph.4 = "Packet lost, slow down"
  165.   icmph.5 = "Shorter route"
  166.   icmph.8 = "Echo service"
  167.   icmph.11 = "Time exceeded"
  168.   icmph.12 = "IP header bad"
  169.   icmph.13 = "Timestamp request"
  170.   icmph.14 = "Timestamp reply"
  171.   icmph.15 = "Information request"
  172.   icmph.16 = "Information reply"
  173.   icmph.17 = "Address mask request"
  174.   icmph.18 = "Address mask reply"
  175.   /* First out, then in statistics */
  176.   say "icmp:"
  177.   do a = 0 to 1
  178.     if a = 0 then do
  179.       say tab || icps_error "calls to icmp_error"
  180.       say tab || icps_oldicmp "errors not generated 'cuz old message was icmp"
  181.       say tab || "Output histogram:"
  182.     end
  183.     else do
  184.       say tab || icps_badcode "messages with bad code fields"
  185.       say tab || icps_tooshort "messages < minimum length"
  186.       say tab || icps_checksum "bad checksums"
  187.       say tab || icps_badlen "messages with bad length"
  188.       say tab || "Input histogram:"
  189.     end
  190.     do b = 0 to 18
  191.       count = subword(icmphist, a*19 + b + 1, 1)
  192.       if count ~= 0 then say tab || tab || count "times" icmph.b
  193.     end /* do b */
  194.   end /* do a */
  195.   say tab || icps_reflect "message responses generated"
  196. end /* end STATUS */
  197.  
  198. if left(args, 5) = "ROUTE" then do
  199.   afamily.1  = "Unix";
  200.   afamily.2  = "Internet Protocols";
  201.   afamily.3  = "Arpanet IMP Addresses";
  202.   afamily.4  = "PUP Protocols";
  203.   afamily.5  = "MIT CHAOS";
  204.   afamily.6  = "XEROX NS ";
  205.   afamily.7  = "ISO Protocols";
  206.   afamily.8  = "ECMA";
  207.   afamily.9  = "Datakit Protocols";
  208.   afamily.10 = "CCITT (X.25) Protocols";
  209.   afamily.11 = "IBM SNA";
  210.   afamily.12 = "DECnet";
  211.   afamily.13 = "DEC Direct Data Link Interface";
  212.   afamily.14 = "LAT";
  213.   afamily.15 = "NSC Hyperchannel";
  214.   afamily.16 = "Apple Talk";
  215.   afamily.17 = "Internal Routing Protocol";
  216.   afamily.18 = "Link layer interface";
  217.   afamily.19 = "eXpress Transfer Protocol";
  218.  
  219.   'QUERY ROUTES ALL' /* Ask information */
  220.  
  221.   oldaf = -1;
  222.   rest = result;
  223.   do while rest ~= ""
  224.     parse value rest with af dest gate flags refs used iface rest
  225.     /* Print address (protocol) family */
  226.     af=x2d(af);
  227.     if af ~= oldaf then do
  228.     /* Print header */
  229.       if af ~= 0 then do 
  230.         say "Routing table for" afamily.af
  231.         say "Destination          Gateway              Flags     Refs     Use  Interface"
  232.       end; else do
  233.     say "Netmasks"
  234.       end
  235.       oldaf = af;
  236.     end
  237.     /* Addresses to decimal dot notation */
  238.     if (af == 2) then do
  239.       if (dest == "00000000") then do
  240.         desta = "default"
  241.       end 
  242.       else do
  243.         desta = x2d(substr(dest,1,2)) || "." || x2d(substr(dest,3,2)) || "." ||,
  244.                 x2d(substr(dest,5,2)) || "." || x2d(substr(dest,7,2))
  245.       end
  246.       gatea = x2d(substr(gate,1,2)) || "." || x2d(substr(gate,3,2)) || "." ||,
  247.               x2d(substr(gate,5,2)) || "." || x2d(substr(gate,7,2))
  248.       /* Print route */
  249.       refs = x2d(refs);
  250.       used = x2d(used);
  251.       say left(resolve(desta), 20) left(resolve(gatea), 20) left(flags, 8),
  252.       right(refs, 4) right(used, 8) "" iface;
  253.       /* right(refs, 5), right(use, 9), interface */
  254.     end 
  255.     else do
  256.       say x2d(substr(dest,1,2)) || "." || x2d(substr(dest,3,2)) || "." ||,
  257.           x2d(substr(dest,5,2)) || "." || x2d(substr(dest,7,2))
  258.     end
  259.   end /* while rest */
  260. end /* ROUTE */
  261. /*EOF*/
  262. exit
  263.  
  264. /*
  265.  * Using the AmiTCP 3.0 'resolve' command, this subroutine tries to convert
  266.  * the ip address into a canonical name of the host.
  267.  * (by Pavel Troller <patrol@k332.feld.cvut.cz>)
  268.  *
  269.  * The resolved names are cached into the "resolved.". If resolving fails,
  270.  * the address is inserted to the cache to prevent resolving the same 
  271.  * address again.
  272.  * (jraja)
  273.  */
  274.  
  275. resolve:
  276. parse arg resadr .
  277. if ~numeric then do
  278.  resname = resolved.resadr
  279.  if resname = '' then do
  280.    address 'COMMAND' 'AmiTCP:bin/resolve >PIPE:netstat'||pragma("ID") resadr
  281.    call open(rfile, 'PIPE:netstat'||pragma("ID"), 'R')
  282.    parse value readln(rfile) with bla bla resname .
  283.    call close(rfile)
  284.  end
  285.  if resname = '' then
  286.    resname = resadr
  287.  /*
  288.   * Add the resolved name to cache
  289.   */
  290.  resolved.resadr = resname
  291.  
  292. end
  293. return resname
  294.